//
//  NewSchedulerViewCell.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/15.
//

import UIKit

class NewSchedulerViewCell: UICollectionViewCell {
    
    private let leftView = UIView()
    private var titleL = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addRoundedCorners(withRadius: 10)
        addUI()
    }
    
    private func addUI() {
        
        leftView.frame = CGRect(x: 0, y: 0, width: 20, height: 50)
//        leftView.backgroundColor = .clear
        self.addSubview(leftView)
        
        titleL = UILabel().then({
            $0.frame = CGRect(x: 25, y: 0, width: 70, height: 50)
            $0.text = ""
            $0.textColor = .white
            $0.font = SYSFONT(ofSize: 14)
        })
        self.addSubview(titleL)
    }
    
    var titleStr = "" {
        didSet {
            var color = UIColor.white
            if titleStr == "饮食计划" {
                color = ColorWithStr("#FF7D83")
            } else if titleStr == "运动计划" {
                color = ColorWithStr("#8393FC")
            } else if titleStr == "情绪控制" {
                color = ColorWithStr("#C783FC")
            } else if titleStr == "月经调节" {
                color = ColorWithStr("#FE4141")
            } else if titleStr == "备孕" {
                color = ColorWithStr("#FFD88B")
            } else if titleStr == "冻卵准备" {
                color = ColorWithStr("#48EB4D")
            }
            titleL.text = titleStr
            self.backgroundColor = color.withAlphaComponent(0.1)
            leftView.backgroundColor = color
            titleL.textColor = color
        }
    }
    
    var model = ScheduleClassModel() {
        didSet {
            titleL.text = model.name
            let colorStr = "\(model.color)"
            let color = UIColor.rgbConversionColorl(rgbStr: colorStr)
            self.backgroundColor = color.withAlphaComponent(0.1)
            leftView.backgroundColor = color
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



extension UIColor {
    
    class func rgbConversionColorl(rgbStr: String) -> UIColor {
        let colorArr = rgbStr.components(separatedBy: ",")
        guard colorArr.count == 3 else {
            return RandomColor()
        }
        let color1 = "\(colorArr[0])"
        let color11 = Int(color1) ?? 0
        let color2 = "\(colorArr[1])"
        let color22 = Int(color2) ?? 0
        let color3 = "\(colorArr[2])"
        let color33 = Int(color3) ?? 0
        let color = UIColor(red: Double(color11) / 255.0, green: Double(color22) / 255.0, blue: Double(color33) / 255.0, alpha: 1)
        return color
    }
    
}
